package com.cwj.mansysback.servlet.impl;

import com.alibaba.fastjson.JSON;
import com.cwj.mansysback.entity.Account;
import com.cwj.mansysback.entity.Student;
import com.cwj.mansysback.entity.Teacher;
import com.cwj.mansysback.service.api.StudentService;
import com.cwj.mansysback.service.api.TeacherService;
import com.cwj.mansysback.service.impl.StudentServiceImpl;
import com.cwj.mansysback.service.impl.TeacherServiceImpl;
import com.cwj.mansysback.servlet.util.MyUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UpdateAccServlet extends HttpServlet {

    TeacherService teaSer = new TeacherServiceImpl();

    StudentService studSer = new StudentServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
//        System.out.println(req.getParameter("teacher"));
        String teaStr = req.getParameter("teacher");

        System.out.println(teaStr);

        Account acc = null;

        boolean flag = true;

        Statement statement = null;

        if(req.getRequestURI().contains("Teacher") && teaStr != null){
            Teacher teacher = JSON.parseObject(teaStr,Teacher.class);
            System.out.println(teacher);
            flag=teaSer.updateTeacher(teacher);

        }else if(req.getRequestURI().contains("/Lock/Update/Acc/Stud")){
            String stud = req.getParameter("student");
            if(stud != null){
                final Student student = JSON.parseObject(stud, Student.class);
                System.out.println(stud);
                System.out.println(student);
                flag = studSer.updateStudent(student);
            }


        }

        if(flag){
            statement = new Statement(0);
        }else{
            statement =new Statement(9);
        }
        MyUtils.writeResponse(resp,statement);


    }
}
